let randomNumber = Math.floor(Math.random()*100)+1;
const yourguess = document.querySelector('.yourguess');
const guesstruth = document.querySelector('.guesstruth');
const guesstip = document.querySelector('.guesstip');
const guessField = document.querySelector('.guessField');
const guessubmit = document.querySelector('.guessubmit');
let guessCount = 1;
let resutButton;
guesstip.style.backgroundColor = 'green';
function checkGuess(){
    let userGuess = Number(guessField.value);
    if(guessCount === 1){
        yourguess.textContent = '上次猜的数：'
    }
    yourguess.textContent += userGuess + ',';
    if(userGuess === randomNumber){
        guesstruth.textContent = '恭喜你猜对了';
        guesstruth.style.backgroundColor = 'green';
        guesstip.textContent = '';
        setGameOver();
    }else if(guessCount ===10){
        guesstruth.textContent = '你猜错了';
        setGameOver();
    }else {
        guesstruth.textContent = '你猜错了';
        guesstruth.style.backgroundColor = 'red';
        if(userGuess < randomNumber){
            guesstip.textContent = '你猜低了';
        }else if (userGuess > randomNumber){
            guesstip.textContent = '你猜高了';
        }
    }
    guessCount++;
    guessField.value = '';
    guessField.focus();
}
guessubmit.addEventListener('click',checkGuess);

function setGameOver(){
    guessField.disabled = true;
    guessubmit.disabled = true;
    resutButton = document.createElement('button');
    resutButton.textContent = '开始新游戏';
    document.body.appendChild(resutButton);
    resutButton.style.margin = '0 46%'
    resutButton.addEventListener('click',resetGame);
}

function resetGame(){
    guessCount = 1;
    const reset = document.querySelectorAll('.result p');
    for(let i = 0;i < reset.length;i++){
        reset[i].textContent = '';
    }
    resutButton.parentNode.removeChild(resutButton);

    guessField.disabled = false;
    guessubmit.disabled = false;
    guessField.value = '';
    guessField.focus();

    guesstruth.style.backgroundColor = 'white';
    randomNumber = Math.floor(Marh.random()*100)+1;
}